//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.Monitor
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation;
    using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
    using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
    using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;

    public class CustomEnterpriseLogger : IDisposable
    {
        private const string CategoryName = "General2";
        
        private LogWriter writer;

        public CustomEnterpriseLogger(string filePath, SourceLevels level)
        {
            this.CreateCustomEnterpriseLogger(filePath, level);
        }

        public void Close()
        {
            this.Dispose();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.writer != null)
                {
                    this.writer.Dispose();
                    this.writer = null;
                }
            }
        }
        
        public void Verbose(string message)
        {
            this.Write(message, TraceEventType.Verbose);
        }

        public void Info(string message)
        {
            this.Write(message, TraceEventType.Information);
        }

        public void Warn(string message)
        {
            this.Write(message, TraceEventType.Warning);
        }

        public void Error(string message)
        {
            this.Write(message, TraceEventType.Error);
        }

        public void Write(string message, TraceEventType severity)
        {
            LogEntry entry = new LogEntry();
            entry.Categories.Add(CategoryName);
            entry.Message = message;
            entry.Severity = severity;

            this.writer.Write(entry);
        }

        private void CreateCustomEnterpriseLogger(string filePath, SourceLevels level)
        {
            LogSource emptyTraceSource = new LogSource("none");

            TextFormatter formatter = new TextFormatter("Message: {message}{newline}");

            FlatFileTraceListener listener = new FlatFileTraceListener(filePath, formatter);

            LogSource generalSource = new LogSource(CategoryName, level);
            generalSource.Listeners.Add(listener);

            Dictionary<string, LogSource> traceSources = new Dictionary<string, LogSource>();
            traceSources.Add(generalSource.Name, generalSource);
            
            this.writer = new LogWriterImpl(new ILogFilter[0], 
                traceSources,
                emptyTraceSource,
                emptyTraceSource,
                generalSource, 
                CategoryName, 
                false, 
                true);
        }
    }
}
